package com.example.fufudamowang.greendao;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.fufudamowang.DaoMaster;
import com.example.fufudamowang.DaoSession;
import com.example.fufudamowang.Note;
import com.example.fufudamowang.NoteDao;

import java.text.DateFormat;
import java.util.Date;

public class Edit extends AppCompatActivity {


    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;
    private Note note;

    private EditText title;
    private EditText body;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        Bundle bundle=this.getIntent().getExtras();
        long id=bundle.getLong("ID");
        InitDAO();
        note=noteDao.loadByRowId(id);

        title=(EditText)findViewById(R.id.title2);
        body=(EditText)findViewById(R.id.body2);
        button=(Button)findViewById(R.id.BB2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String note_title=title.getText().toString();
                String note_body=body.getText().toString();
                note.setTitle(note_title);
                note.setBody(note_body);
                final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                        DateFormat.MEDIUM);
                String date = df.format(new Date());
                note.setDate(date);
                noteDao.update(note);
                Toast.makeText(Edit.this,"修改成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
        title.setText(note.getTitle());
        body.setText(note.getBody());
    }





    private void InitDAO() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
                null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        noteDao = daoSession.getNoteDao();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu2,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==R.id.menu2)
            shareNote();
        return super.onOptionsItemSelected(item);
    }

    private void shareNote(){
        Intent share_intent = new Intent();
        share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
        share_intent.setType("text/plain");//设置分享内容的类型
        if (note==null){
            Toast.makeText(Edit.this,"此日记尚未保存,请保存后再进行分享操作",Toast.LENGTH_SHORT).show();
        }
        else {

            share_intent.putExtra(Intent.EXTRA_TEXT, note.getBody());
            share_intent = Intent.createChooser(share_intent,"分享"+note.getTitle()+"的内容");
            startActivity(share_intent);
        }
    }
}
